<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<script type="text/javascript">
   $(function(){
   	 xf.ns('xf.admin.menu${OP.menuId}');
   	 $('#admin_menu${OP.menuId}_datagrid').bootstrapTable({ 
          url: rootPath + '${MENU.menuUrl}/selectSysMenuList.do', 
          height: '100%',
          sortName: 'name',
          sortOrder: 'desc',
          striped: true,
          pagination: true,
          pageSize: 10,
          pageList: [10, 25, 50, 100, 200],
          search: false,
          sidePagination:'server',
          idField: 'id',
          uniqueId: 'id',
          responseHandler: responseHandler,
          queryParams: queryParams,
          minimumCountColumns: 2,
          clickToSelect: true,
          searchAlign: 'left',
          columns: [{
              checkbox: true
          }, {
              field: 'menuId',
              title: '编号',
              visible : false
          },{
              field: 'menuName',
              title: '菜单名称',
              align: 'center',
              valign: 'middle',
              sortable: true,
          },{
              field: 'menuUrl',
              title: '菜单地址',
              align: 'center',
              valign: 'middle'
          },{
              field: 'menuImg',
              title: '菜单图标',
              align: 'center',
              valign: 'middle'
          },{
              field: 'menuParam',
              title: '菜单参数',
              align: 'center',
              valign: 'middle'
          },{
              field: 'parentId',
              title: '父菜单',
              align: 'center',
              valign: 'middle'
          },{
              field: 'level',
              title: '排序',
              align: 'center',
              valign: 'middle'
          },{
              field: 'isShow',
              title: '是否展示',
              align: 'center',
              valign: 'middle'
          },{
              field: 'createDate',
              title: '创建日期',
              align: 'center',
              valign: 'middle'
          },{
              field: 'createTime',
              title: '创建时间',
              align: 'center',
              valign: 'middle'
          },{
              field: 'status',
              title: '状态',
              align: 'center',
              valign: 'middle'
          } ]
    });
})
    
function stateFormatter(value, row, index) {
    if (value == 1) {
    	return '<i class="fa fa-check text-navy"></i>';
    }else{
    	return '<i class="fa fa-close text-danger"></i>';
    }
    return value;
}

//传递的参数
function queryParams(params) {
	var pageSize = params.limit;
	var sort = params.sort;
	var offset = params.offset;
	var order = params.order;
	var pageNum = offset / pageSize + 1;
	var temp={
   		pageSize : pageSize,
   		pageNum : pageNum,
   		sort : sort,
   		order : order
    }
    var data=serializeForm("admin_menu${OP.menuId}_formSearch");
    $.extend(temp,data);
    return temp;
}
//查询
xf.admin.menu${OP.menuId}.serach = function(){
	 $('#admin_menu${OP.menuId}_datagrid').bootstrapTable('refresh');
}

//清空查询条件输入域
xf.admin.menu${OP.menuId}.clearForm=function(){
	$("#admin_menu${OP.menuId}_formSearch")[0].reset();
}

//新增
xf.admin.menu${OP.menuId}.add = function(){
	xf.showWindow({
			title:'增加菜单',
			href:rootPath + '/op_edit_${OP.menuId}.do',
			width:'50%',
			height:'80%',
			okhandler:function(){
				xf.admin.menu${OP.menuId}.save();
			},
			cancelhandler:function(){ 
				xf.closeWindow();
			}
		}); 
	
}

//编辑
xf.admin.menu${OP.menuId}.edit = function(){
	var rows =$('#admin_menu${OP.menuId}_datagrid').bootstrapTable('getSelections');
	if(rows.length==0){
		xf.toastrsAlert({
		     code:'info',
		     msg:'请选择你要编辑的记录'
		});
		return;
	}
	if(rows.length > 1){
		xf.toastrsAlert({
		     code:'warning',
		     msg:'sorry , 只能选择一条进行编辑'
		});
		return;
	}
	xf.showWindow({
			title:'编辑菜单',
			href:rootPath + '/op_edit_${OP.menuId}.do?menuId='+rows[0].menuId,
			width:'50%',
			height:'80%',
			okhandler:function(){
				xf.admin.menu${OP.menuId}.update();
			},
			cancelhandler:function(){
				xf.closeWindow();
			}
		});
}

//删除
xf.admin.menu${OP.menuId}.del = function(){ 
	var rows =$('#admin_menu${OP.menuId}_datagrid').bootstrapTable('getSelections');
	if(rows.length==0){
		xf.toastrsAlert({
		     code:'info',
		     msg:'请选择你要删除的记录'
		});
		return;
	}
	xf.confirm(function(){
		var ps = [];
    	$.each(rows,function(i,n){
    		ps.push(n.menuId);
    	});
    	$.ajax({
            type: 'post',
            url: rootPath + '/op_remove_${OP.menuId}.do',
            data: {"ids":ps.join(",")},
            dataType: 'json',
            success: function (data) {
            	$('#admin_menu${OP.menuId}_datagrid').bootstrapTable('refresh');
            	xf.toastrsAlert({
              		 code: data.success ? 'success' :'error',
    		       	 msg: data.success ? '成功' :'失败'
         		});
            }
        });
	});
}
//--------------------------------------------------------------------------------
$(function(){
// 	document.getElementById("admin_menu${OP.menuId}_formSearch").style.display='none';
	$("#admin_menu${OP.menuId}_formSearch").hide();
});

//---------------------------------------------------------------------------------
var zNodes;
  var zTreeObj;
  var setting = {
    view: {
        addHoverDom: addHoverDom,
        removeHoverDom: removeHoverDom,
        selectedMulti: false
    },
  	check: {
  		enable: false,//显示checkbox
  		chkStyle: "checkbox",
  		chkboxType: { "Y": "p", "N": "s" }
  	},
  	data: {
		key: {
// 			children: "id",
// 			name: "roleName"
		}
	},
	edit: {
        enable: true
    },
  	callback: {
  		onClick: zTreeOnClick
	}
  };
  
  $(function(){
	 $.ajax({
		type: "POST", 
		url: rootPath + "/sysMenu/selectParentMenu.do",
		data: '',
		dataType: "json",
		success: function(data){
			zNodes=data;
        	zTreeObj = $.fn.zTree.init($("#menuTree"), setting, zNodes); 
		}
	});
  });
  
  function zTreeOnClick(event, treeId, treeNode) {
	  $("#menuName").val(treeNode.name);
	  $('#admin_menu${OP.menuId}_datagrid').bootstrapTable('refresh');
  };
  
  var newCount = 1;
  function addHoverDom(treeId, treeNode) {
      var sObj = $("#" + treeNode.tId + "_span");
      if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
      var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
          + "' title='add node' onfocus='this.blur();'></span>";
      sObj.after(addStr);
      var btn = $("#addBtn_"+treeNode.tId);
      if (btn) btn.bind("click", function(){
          var zTree = $.fn.zTree.getZTreeObj("menuTree");
          zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, name:"new node" + (newCount++)});
          return false;
      });
  };
  function removeHoverDom(treeId, treeNode) {
      $("#addBtn_"+treeNode.tId).unbind().remove();
  };
</script>

<div class="wrapper wrapper-content animated fadeInRight">
	<div class="ibox float-e-margins">
		<div class="ibox-content">
<!-- 			<div class="panel panel-default"> -->
<!-- 			<div class="panel-heading">查询条件</div> -->
				<div class="panel-body">
					<form id="admin_menu${OP.menuId}_formSearch" class="form-horizontal">
						<div class="form-group" style="margin-top: 15px">
							<label class="control-label col-sm-1" for="txt_search_departmentname">菜单名称</label>
							<div class="col-sm-2">
								<input type="text" class="form-control" name="menuName" id="menuName">
							</div>
							<div class="col-sm-12" style="text-align: right;">
								<button type="button"  onclick = "xf.admin.menu${OP.menuId}.serach();"
									class="btn btn-primary"><i class="fa fa-search"></i>查询</button>
							    <button type="button" class="btn btn-white" onclick="xf.admin.menu${OP.menuId}.clearForm();"><i class="fa fa-eraser"></i>重置</button>
							</div>
						</div>
					</form>
				</div>
<!-- 			</div> -->
			
			<div class="row">
				<div class="col-lg-3">
				<div class="panel panel-default">
				    <div class="panel-heading">菜单</div>
					<div class="form-group">
					    <ul id="menuTree" class="ztree"></ul>
				    </div>
			    </div>
				</div>
			
				<div class="col-lg-9">
				<%@ include file="/WEB-INF/views/common/toolbar.jsp"%>
					<div class="panel-body" style="padding:0;">
						<div class="panel panel-default">
							<div class="panel-heading">列表</div>
							<div class="panel-body">
								<table id="admin_menu${OP.menuId}_datagrid" data-toolbar="#toolbar"
									data-show-refresh="true" data-show-toggle="true"
									data-show-columns="true" data-show-footer="false"
									data-mobile-responsive="true">
								</table>
							</div>
						</div>
					</div>
			  </div>
		</div>
	</div>
</div>